<template>
  <div :class="isComponet === true ? '' : 'app-container'">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="66px">
        <el-form-item label="设备类型">
          <el-select
            v-model="searchParam.equipTypeId"
            placeholder="请选择设备类型"
            clearable
            @keyup.enter="search"
          >
            <el-option
              v-for="item in equipTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="生产设备">
          <el-select
            v-model="searchParam.processEquipId"
            placeholder="请选择生产设备"
            clearable
            @keyup.enter="search"
          >
            <el-option
              v-for="item in equipProduceList"
              :key="item.id"
              :label="item.equipCode"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="位置">
          <el-input
            v-model="searchParam.locationId"
            placeholder="请输入位置"
            clearable
            @keyup.enter="search"
          />
        </el-form-item> -->
        <el-form-item label="设备编号">
          <el-input
            v-model="searchParam.equipCode"
            placeholder="请输入设备编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input
            v-model="searchParam.equipName"
            placeholder="请输入设备名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <!-- <template #header>
        <el-button
          @click="
            useDownload(
              '/pqd-etl/equfaultinfo/export',
              '设备故障信息',
              searchParam
            )
          "
        >
          <template #icon><i-ep-download /></template>导出</el-button
        >
      </template> -->

      <!-- 卡片内容 -->
      <div
        class="bg-#fff rounded-[8px]"
        style="min-height: 250px"
        v-loading="loading"
      >
        <el-empty
          v-if="!tableData.length && !loading"
          :image-size="200"
          :image="pic3"
        />

        <div class="table-container-scroll">
          <el-row class="cardContainer" :gutter="12">
            <el-col
              :span="cardSpan"
              v-for="(self, selfIndex) in tableData"
              :key="selfIndex"
            >
              <div class="card-item-li">
                <div class="card-item">
                  <div class="card-white-title">
                    <span class="text-ellipsis">{{ self.equipName }}</span>
                  </div>

                  <!-- <span>{{ self.faultText }}</span> -->
                  <div class="card-content">
                    <div class="card-right">
                      <p class="card-name">
                        <span>设备编号</span
                        ><span class="numberLight w-[170px] text-ellipsis">{{
                          self.equipCode
                        }}</span>
                      </p>
                      <p class="card-name">
                        <span>生产设备</span
                        ><span class="numberLight w-[170px] text-ellipsis">{{
                          self.processEquipCode || ""
                        }}</span>
                      </p>
                      <p class="card-name">
                        <span>最后采集时间</span
                        ><span class="numberLight w-[170px] text-ellipsis">{{
                          self.lastCollectTime || "--"
                        }}</span>
                      </p>
                    </div>
                  </div>

                  <div class="card-footer">
                    <!-- <el-button
                      class="grey-button"
                      @click="openDialogdetail(self)"
                      >模型定义</el-button
                    > -->
                    <el-button
                      v-if="assetType == 2"
                      class="grey-button"
                      @click="openDialogdetail(self, 2, '加工参数')"
                      >加工参数</el-button
                    >
                    <el-button
                      v-if="assetType == 2"
                      class="blue-button"
                      @click="openDialogdetail(self, 3, '过程参数')"
                      >过程参数</el-button
                    >
                    <el-button
                      v-if="assetType == 1"
                      class="blue-button"
                      @click="openDialogdetail(self, 1, '运行参数')"
                      >运行参数</el-button
                    >
                  </div>
                </div>
              </div>
            </el-col>
          </el-row>
        </div>
      </div>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[20, 40, 60, 80, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <equOperatingParameters-last
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />

    <!-- <equOperatingParameters-detail
      :visible="listDialog.visible"
      :title="listDialog.title"
      :data="listDialog.data"
      :assetType="assetType"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="listDialog.readvisible"
      @change-visible="changeVisible"
    /> -->
  </div>
</template>

<script setup lang="ts">
import { useTable } from "@/utils/useTable";
import pic3 from "@/assets/images/noData2.png";
import request from "@/utils/request";

defineOptions({
  name: "CDBEQUOPERATINGPARAMETERS",
  inheritAttrs: false,
});

const props = defineProps({
  isComponet: {
    type: Boolean,
    default: false,
  },
  // 1 设备运行参数  2、工艺数据
  assetType: {
    type: Number,
    default: 1,
  },
});

const { isComponet, assetType } = toRefs(props);
// 请求参数
const requestApi = "/iotadmin/deviceMonitor/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  equipCode: "",
  equipName: "",
  equipTypeId: "",
  locationId: "",
  processEquipId: "",
  enabled: 1,
  // assetType: assetType.value,
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError
);

onMounted(() => {
  pageable.value.pageSize = 20;
  getTableList();
  getEquipTypeList();
  getEquipProduceList();
  // 在组件挂载后检查屏幕大小
  // 添加事件监听，以便在屏幕大小变化时更新条件
  window.addEventListener("resize", checkScreenSize);
});

// 运行参数详情弹窗定义
const listDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 打开设备运行参数列表弹窗
function openDialogList(roleObj?: any, paramType?: number, title?: string) {
  listDialog.title = title + "详情";
  listDialog.data = {
    id: roleObj.id,
    equipCode: roleObj.equipCode,
    paramType: paramType,
  };
  listDialog.visible = true;
  listDialog.readvisible = true;
}

// 模型参数详情弹窗定义
const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string, paramType: any) {
  request({
    url: `/iotadmin/deviceMonitor/getDeviceInfo/${id}?paramType=${paramType}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = {
        ...data.data,
        paramType,
      };
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开设备故障信息表单弹窗 详情 */
function openDialogdetail(roleObj: any, paramType: any, text: any) {
  addDialog.title = roleObj?.equipName + text + "详情";
  getInfo(roleObj.equipCode, paramType);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  listDialog.visible = value;
  if (!value) {
    addDialog.data = {};
    listDialog.data = {};
  }
}

const cardSpan = ref(changeSpan(window.innerWidth));

onUnmounted(() => {
  // 清理事件监听器
  window.removeEventListener("resize", checkScreenSize);
});

const checkScreenSize = () => {
  cardSpan.value = changeSpan(window.innerWidth);
};

// 共60份  60/2
function changeSpan(innerWidth: number) {
  if (innerWidth < 768) {
    return 12;
  } else if (innerWidth >= 768 && innerWidth < 992) {
    return 12;
  } else if (innerWidth >= 992 && innerWidth < 1200) {
    return 8;
  } else if (innerWidth >= 1200 && innerWidth < 1500) {
    return 8;
  } else if (innerWidth >= 1500 && innerWidth < 1920) {
    return 6;
  } else if (innerWidth >= 1920) {
    return 5;
  }
}

// 获取设备类型下拉框
const equipTypeList: any = ref([]);
const getEquipTypeList = async () => {
  const res: any = await request({
    url: "/iotadmin/scaequiptype/getlist",
    method: "post",
    data: {},
  });
  if (res.code === 200) {
    equipTypeList.value = res.data;
    equipTypeList.value?.map((item: any) => {
      item.label = `${item.equipTypeCode}-${item.equipTypeName}`;
      item.value = `${item.id}`;
    });
  }
};

// 获取生产设备下拉框
const equipProduceList: any = ref([]);
const getEquipProduceList = async () => {
  const res: any = await request({
    url: "/mdm/mdmprocessequip/getList",
    method: "post",
    data: {},
  });
  if (res.code === 200) {
    equipProduceList.value = res.data;
  }
};
</script>
<style lang="scss" scoped>
@import url("@/styles/cardlist.scss");
</style>
